<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class jx_tarifas extends CI_Controller {

    private $establecimiento;
    private $data;

    public function __construct() {
        parent::__construct();
        session_start();
        $this->establecimiento = $_SESSION["establecimiento"]["id_establecimiento"];
        $this->load->model('tarifasM');
        $this->load->model('temporadasM');
        $this->load->model('habitacionesM');
    }

    public function modificar() {
        if (!empty($_POST)) {
            $categoria = $_POST['categoria'];

            echo $this->createTable($categoria);
        }
    }

    public function createTable($categoria) {
        $this->data = array();
        $this->loadData($categoria);
        if ($this->hasDataLoaded()) {
            $table = $this->initContent();
            $table.= $this->getInitTable();
            $table.= $this->getHeader();
            $table.=$this->getBodyPersonas();
            $table.=$this->getBodyExtra();
            $table .= $this->getEndTable();
            $table.=$this->closeContent();
        } else {
            $table = $this->findErrors();
        }
        return $table;
    }

    private function hasDataLoaded() {
        $ntemporadas = sizeof($this->data['temporadas']);
        $ncuartos = sizeof($this->data['cuartos']);
        return ($ntemporadas > 0 && $ncuartos > 0 && $this->data['simbolo'] != null);
    }

    private function loadData($categoria) {
        $this->data["temporadas"] = $this->temporadasM->getTemporadas($this->establecimiento);
        $this->data["categoria"] = $this->habitacionesM->getCategoria($categoria);
        $this->data["cuartos"] = $this->habitacionesM->getCuartos($categoria);
        $this->data['simbolo'] = $this->tarifasM->getSimbolo($this->establecimiento);
        $this->data['simbolo'] = $this->data['simbolo'][0]->simbolo;
        $this->data["data1"] = $this->tarifasM->getCategoriaTemporada($categoria);
//carga los datos de las tarifas por persona
        $this->data["tarifaPersona"] = $this->tarifasM->getTarifaPersona($categoria);
    }

    private function initContent() {
        return "<form action='tarifas/guardar' method='post'>
    <input type='hidden' name='id_categoria' value='{$this->data['categoria'][0]->id_categoria}'>";
    }

    private function getInitTable() {
        return "
                 <div class='row-fluid' style='overflow: scroll;'>
            <table  class='table table-bordered table-hover table-striped table-condensed'>";
    }

    public function getHeader() {
        $header = "<thead>
            <tr>
                <td></td>";
        foreach ($this->data['temporadas'] as $temporada)
            $header.= "<td> <center><strong>{$temporada->nombre}</strong></center></td>";

        $header.= "</tr>    
        </thead>";
        return $header;
    }

    private function getBodyPersonas() {
        $body = "";
        for ($i = 1; $i <= $this->data['categoria'][0]->capacidad_estandar; $i++) {
            $body.="<tbody><tr>
                <td>
        <center>Para {$i} personas</center>
        </td>";
            foreach ($this->data['temporadas'] as $temporada) {
                foreach ($this->data['tarifaPersona'] as $tp) {
                    if ($tp->n_personas == $i && $tp->id_temporada == $temporada->id_temporada) {
                        $body.="<td>
            <div class='input-prepend'>
                <center>
                    <span class='add-on'>{$this->data['simbolo']}</span>                                                                            
                    <input class='input-small precio' name='tarifa-{$i}-{$temporada->id_temporada}' type='text' value='" . (float) $tp->valor . "' />
                </center>   
            </div>
        </td>";
                    }
                }
            }
            $body.="</tr>";
        }

        return $body;
    }

    private function getBodyExtra() {
        $body = "";
        // var_dump($_POST['privada']);
        if ($_POST['privada'] == "true" && $_POST['adicional'] != 0) {
            $body .= "<tr>
            <td><center>Persona adicional</center></td>";
            foreach ($this->data['data1'] as $temporada) {
                $body.="<td>
            <div class='input-prepend'>
                <center>
                    <span class='add-on'>{$this->data['simbolo']}</span>       
                    <input class='input-small precio' name='dia-tarifa_persona_adicional-{$temporada->id_temporada}' value='" . (float) $temporada->tarifa_persona_adicional . "'  class='dia' data-adicional='' type='text' required/>
                </center>
            </div>
        </td>";
            }
        }
        $body.="</tr> 
        <tr>
            <td><center>Incremento viernes</center></td>";

        foreach ($this->data['data1'] as $temporada) {

            $body.="<td> <div class='input-prepend'>
                <center>
                    <span class='add-on'>{$this->data['simbolo']}</span> <input class='input-small precio' name='dia-incremento_viernes-{$temporada->id_temporada}'  class='dia' value='" . (float) $temporada->incremento_viernes . "' type='text' required />
                </center>
            </div>     
        </td>";
        }
        $body.="</tr> 
        <tr>
            <td><center>Incremento sábado</center></td>";
        foreach ($this->data['data1'] as $temporada) {
            $body.="<td>
            <div class='input-prepend'>
                <center>
                    <span class='add-on'>{$this->data['simbolo']}</span>   
                    <input class='input-small precio' name='dia-incremento_sabado-{$temporada->id_temporada}' value='" . (float) $temporada->incremento_sabado . "' class='dia'  type='text' required/>
                </center>
            </div>
        </td>";
        }
        $body.="</tr> 

        <tr>
            <td><center>Incremento domingo</center></td>";
        foreach ($this->data['data1'] as $temporada) {
            $body.="<td>
            <div class='input-prepend'>
                <center>
                    <span class='add-on'>{$this->data['simbolo']}</span>   
                    <input class='input-small precio' name='dia-incremento_domingo-{$temporada->id_temporada}'  value='" . (float) $temporada->incremento_domingo . "' class='dia' type='text' required/>
                </center> 
            </div>
        </td>";
        }
        $body.="</tr> 
        </tbody>";
        return $body;
    }

    private function getEndTable() {
        return "</table>";
    }

    private function closeContent() {
        return "<input type='submit' class='btn btn-primary' name='guardar' value='Guardar' />
</form></div>";
    }

    private function findErrors() {
        $errors = "";
        if (sizeof($this->data['temporadas']) == 0) {
            $errors.= "<div class='alert alert-error'>
                                        <center><span> <i>Debes <a href='temporadas'>crear</a> al menos una temporada</i></span> </center>
                                    </div>  ";
        }
        if (sizeof($this->data['cuartos']) == 0) {
            $errors.= "<div class='alert alert-error'>
                                        <center><span> <i>Debes <a href='habitaciones'>crear</a> al menos una habitacion</i></span> </center>
                                    </div>  ";
        }

        if ($this->data['simbolo'] == null) {
            $errors.="<div class='alert alert-error'>
            <center><span> <i>Debes <a href='informacionComercial'>usar</a> al menos una moneda</i></span> </center>
        </div>";
        }

        return $errors;
    }

}
